<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background-color:#F7F8FA; height:100%; }
			.footer { background-color:#fff; position: fixed; left: 0; right: 0; bottom: 0px; height: 80px; }
			.footer div { width:100%; padding: 15px; box-sizing:border-box;}
			.footer div b { font-size: 16px; color:#333; font-weight:500; }
			.footer div p { font-size: 13px;  color:#666; font-weight:400;word-wrap:break-word; }
		</style>
	</head>
	<body>
		<div id="map"></div>
		<div class="footer">
			
			<div>
				<b>[位置]</b>
				<p id="myposition">获取中...</p>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js" ></script>
	<script type="text/javascript" src="../../script/app.js" ></script>
	<script type="text/javascript">
		var aMap;
		var lon,lat,address;
		var mapHeight,imgpath;
		apiready = function() {
			api.parseTapmode();
			mapHeight = api.winHeight - 80;
			$api.css($api.byId('map'),'height:'+mapHeight+'px');
			var imgname = randomString(32);
			var imageCachePath = api.cacheDir;
			imgpath = imageCachePath + '/' + imgname + '.jpg';
			//imgpath = 'fs://aMap/' + imgname + '.png';
			var mapPos = $api.offset($api.byId('map'));
			aMap = api.require('aMap');
			setTimeout(function(){
				aMap.open({
					rect: {
						x: 0,
						y: api.safeArea.top + 45,
						w: 'auto',
						h: mapPos.h
					},
					showUserLocation: true,
					zoomLevel: 14,
					center: {
						lon: 116.4021310000,
						lat: 39.9994480000
					},
					//fixedOn: api.frameName,
					fixed: true
				}, function(ret, err) {
					if (ret.status) {
						aMap.showUserLocation({
							isShow: true,
							imagePath: 'widget://image/mapmarker.png',
							imageSize: {
								w: 40,
								h: 40
							}
						});
						aMap.getLocation(function(ret, err) {
							if (ret.status) {
								lon = ret.lon;
								lat = ret.lat;
								aMap.setCenter({
									coords: {
										lon: lon,
										lat: lat
									},
									animation: false
								});
								//aMap.close();
								aMap.getNameFromCoords({
									lon: lon,
									lat: lat
								}, function(ret, err) {
									if (ret.status) {
										aMap.takeSnapshotInRect({
											rect: {
												x: 0,
												y: 0,
												//y: mapHeight / 2 - 120,
												//w: 360,
												//h: 240
											},
											path: imgpath
										}, function(ret) {
											//alert(JSON.stringify(ret));
										});
									
										address = ret.address;
										$api.text($api.byId('myposition'),address);
									} else {
										$api.text($api.byId('myposition'),'未能获取位置信息');
										//alert(JSON.stringify(err));
									}
								});
								
							
								//alert(JSON.stringify(ret));
							} else {
								//alert(JSON.stringify(err));
								//aMap.close();
							}
						});
						//
					} else {
						//alert(JSON.stringify(err));
						//aMap.close();
					}
				});
			}, 300);
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
				else if (ret.type == 'right') {
					sendPosition();
                }
            });
		}
		function randomString(len) {
		　　len = len || 32;
		　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
		　　var maxPos = $chars.length;
		　　var pwd = '';
		　　for (i = 0; i < len; i++) {
		　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
		　　}
		　　return pwd;
		}
		function sendPosition() {
			//发送定位消息
			var param = {
				lon: lon,
				lat: lat,
				address: address,
				imgpath: imgpath
			}
			api.execScript({
				name: 'chat_win',
				frameName: 'chat_frm',
				script: 'sendPosition(\''+$api.jsonToStr(param)+'\');'
			});
			api.closeWin({
			});
		}
	</script>
</html>